﻿package monopoly.views {

		import flash.events.Event;
		import flash.events.MouseEvent;
		import flash.display.Sprite;
		import flash.display.MovieClip;
		import flash.display.DisplayObject;
		import flash.display.Stage;
		import flash.text.TextField;
		import fl.controls.TextArea;		// added
		import fl.controls.TextInput;		// added
		import fl.transitions.Tween;
		import fl.transitions.easing.*;
		import fl.transitions.TweenEvent;
		import monopoly.chat.*;
		
		public class ChatViewer 
			{
			private var mainStage:Stage;
			private var chat:ChatClient;
			private var origSW:Number;
			private var origSH:Number;
			private var b:Sprite;
			private var bgwidth:Number;
			private var clBox:Sprite;
			private var chatDrag:Boolean;
			private var isHidden:Boolean;

			private var CHATWINX:Number = 383;
			private var CHATWINY:Number = 267;
			
			public function ChatViewer(chat:ChatClient, rootSprite:Sprite) 
				{
				this.chat = chat;

				this.mainStage = rootSprite.root.stage;
				
				chat.setup(rootSprite.width, rootSprite.height);
				 
				this.origSW = this.chat.width;		// original Sprite Width
				this.origSH = this.chat.height;		// original Sprite Height

				CHATWINX = chat.x;
				CHATWINY = chat.y;
				
				chat.x = 0;
				chat.y = 0;
				
				this.prepBox();
				}
		
		private function prepBox() 
			{
			var wid:Number = origSW + 2;
			var hei:Number = origSH + 2;
			this.b = new Sprite();

			with (this.b.graphics) {
				lineStyle(1,0x333333,100,true);
				moveTo(0,0);
				beginFill(0x999999,70);
				lineTo(wid,0);
				lineTo(wid,hei);
				lineTo(0,hei);
				lineTo(0,0);
				}

			this.b.x = this.b.root + CHATWINX;
			this.b.y = this.b.root + CHATWINY;
			
			this.b.name = "chatWindow";
			this.bgwidth = wid;
			this.clBox = new Sprite();
			with (this.clBox.graphics) {
				moveTo(0,0);
				lineStyle(1,0x000000,100);
				beginFill(0xCCCCCC,100);
				lineTo(20,0);
				lineTo(20,20);
				lineTo(0,20);
				lineTo(0,0);
				endFill();
				lineStyle(3,0x000000,100);
				moveTo(3,10);
				lineTo(17,10);
				}
			this.clBox.x = wid-16;
			this.clBox.y = -4;
			this.clBox.addEventListener(MouseEvent.MOUSE_OVER,drawCLDark);
			this.clBox.addEventListener(MouseEvent.MOUSE_OUT,drawCLLite);
			this.clBox.addEventListener(MouseEvent.CLICK,clickCL);
			this.b.addChild(this.clBox);
			
			this.b.addEventListener(MouseEvent.MOUSE_DOWN,dragWin,false,0,true);
			this.mainStage.addEventListener(MouseEvent.MOUSE_UP,stopDragWin,false,0,true);
			this.b.addEventListener(Event.ENTER_FRAME,pushToTop,false,0,true);
			
			this.b.addChild(chat);
			
			this.drawAll();
			}
		
		private function drawAll() 
			{
			this.mainStage.addChild(this.b);
			this.b.buttonMode = true;
			}
		
		private function dragWin(event:Object)
			{
			if (event.target == this.b || event.target.name == "meLabel")
				{
				this.b.startDrag();
				}
			else if (event.target == this.chat)
				{
				this.chatDrag = true;
				this.b.addEventListener(Event.ENTER_FRAME,resizeWin);
				} 
			}
			
		private function stopDragWin(event:Object)
			{
			this.b.stopDrag();
			if (this.chatDrag == true)
				{
				this.b.removeEventListener(Event.ENTER_FRAME,resizeWin);
				this.chatDrag = false;
				this.resizeWin(this.b);
				}
			
			}
			
		private function resizeWin(event:Object)
			{
			var wid:Number = this.b.mouseX;
			var hei:Number = this.b.mouseY;			
			if (wid<400) 
				{
				wid = 400;
				}
			this.bgwidth = wid;
			if (hei<170)
				{
				hei = 170;
				}
			
			this.clBox.x = wid-16;

			chat.ChatArea.height = hei-30;
			chat.ChatArea.drawNow();
			
			with (this.b.graphics)
				{
				clear();
				lineStyle(1,0x333333,100,true);
				moveTo(0,0);
				beginFill(0x999999,70);
				lineTo(wid,0);
				lineTo(wid,hei);
				lineTo(0,hei);
				lineTo(0,0);
				}
			}
			
		private function pushToTop(event:Object)
			{
			//Force the viewer to the top of the Stage -- no matter what//
			if (this.mainStage.contains(b))
				{
				this.mainStage.setChildIndex(this.b,this.mainStage.numChildren-1);
				}
			}
		
		private function clickCL(event:Object) {
			if (!this.isHidden) {
				this.isHidden = true;
				this.mainStage.removeChild(this.b);
				this.drawCLLite(this.b);
				this.clBox.x += this.b.x;
				this.clBox.y += this.b.y;
				var clxTweenOut:Tween = new Tween(this.clBox,"x",None.easeOut,this.clBox.x,this.b.root+CHATWINX+origSW-6,.3,true);
				var clyTweenOut:Tween = new Tween(this.clBox,"y",None.easeOut,this.clBox.y,CHATWINY-10,.3,true);
				this.mainStage.addChild(this.clBox);
			} else {
				this.isHidden = false;
				var clxTweenIn:Tween = new Tween(this.clBox,"x",None.easeOut,this.clBox.x,this.b.x+this.b.width-16,.3,true);
				var clyTweenIn:Tween = new Tween(this.clBox,"y",None.easeOut,this.clBox.y,this.b.y-4,.3,true);
				clyTweenIn.addEventListener(TweenEvent.MOTION_FINISH,clTD);
			}
		}
		private function clTD(event:Object) {
			this.mainStage.removeChild(this.clBox);
			this.drawCLLite(this.b);
			this.clBox.x = this.b.width-16;
			this.clBox.y= -4;
			this.b.addChild(this.clBox);
			this.mainStage.addChild(this.b);
		}
		private function drawCLLite(event:Object) {
			with (this.clBox.graphics) {
				clear
				moveTo(0,0);
				lineStyle(1,0x000000,100);
				beginFill(0xCCCCCC,100);
				lineTo(20,0);
				lineTo(20,20);
				lineTo(0,20);
				lineTo(0,0);
				endFill();
				lineStyle(3,0x000000,100);
				moveTo(3,10);
				lineTo(17,10);
				if (this.isHidden) {
					moveTo(10,3);
					lineTo(10,17);
				}
			}
		}
		private function drawCLDark(event:Object) {
			with (this.clBox.graphics) {
				clear
				moveTo(0,0);
				lineStyle(1,0x000000,100);
				beginFill(0x999999,100);
				lineTo(20,0);
				lineTo(20,20);
				lineTo(0,20);
				lineTo(0,0);
				endFill();
				lineStyle(3,0x000000,100);
				moveTo(3,10);
				lineTo(17,10);
				if (this.isHidden) {
					moveTo(10,3);
					lineTo(10,17);
				}
			}
		}
		public function destroy():void {
			mainStage.contains(b) ? mainStage.removeChild(b) : mainStage.removeChild(clBox);
			b.removeEventListener(Event.ENTER_FRAME,pushToTop);
			mainStage.removeEventListener(MouseEvent.MOUSE_UP,stopDragWin);
		}
	}
}